#include "speedometercontroller.h"


SpeedometerController::SpeedometerController(QObject *parent)
    : QObject{parent}
{
    speedometerView = new SpeedometerView();

    //停留时间初始化
    setDwellTime(QString::number(dwellTime));
    //给定时器实例化对象
    obj_timer = new QTimer(this);
    //将定时器发射的timeout信号与自定义的槽函数进行连接
    connect(obj_timer, &QTimer::timeout, this, &SpeedometerController::timeout_slot);


    distance_timer = new QTimer(this);
    connect(distance_timer, &QTimer::timeout, this, &SpeedometerController::distance_slot);


    //仪表盘
    speedometerView->stopWatchView->operationalLabel->setText(CurrentOperationalList[CurrentOperationalMode::CM]);
    speedometerView->stopWatchView->departLabel->setPixmap(QPixmap(":images/train_depart.png"));

}

void SpeedometerController::show(QVBoxLayout *vLayout, int w)
{
    vLayout->addWidget(speedometerView,w);
}

void SpeedometerController::setTrainRunState(TrainRunState state,int dwellTime,int distance)
{

    switch (state) {
    case TrainRunState::RUNNING:
        setDwellTime("");
        break;
    case TrainRunState::STATION:
        setDwellTime("-");
        setDistanceString(distance);
        distanceTime = distance;
        distance_timer->start(500);
        break;
    case TrainRunState::COUNTDOWN:
        this->dwellTime = dwellTime;
        //测试速度进度条
        setDistanceString(distance);
        distanceTime = distance;
        distance_timer->start(500);
        //更新时间
        setDwellTime(QString::number(dwellTime));
        obj_timer->start(1000);
        break;
    default:
        break;
    }
}

void SpeedometerController::setDistanceString(int distance)
{
    if(distance < 0) return;

    speedometerView->targetView->distanceString->setText(QString::number(distance) + " m");
    setProgressBar(distance);
}

void SpeedometerController::setProgressBar(int distance)
{
    int barValue = 0;
    if(distance > 300){
        barValue = 100;
    }else if(distance <= 300 && distance >= 200){
        barValue =90+ (distance- 200)/10;
    }else if (distance <= 200 && distance >= 100){
        barValue = 60 + ((distance - 100)/10)*3;
    }else {
        barValue = distance/10 * 6;
    }
    speedometerView->targetView->progressBar->setValue(barValue);
    speedometerView->targetView->update();
}

void SpeedometerController::setStationLabel(float station)
{
    QString stationStr = "到站距离\n" + QString::number(station) + "m";
    speedometerView->targetView->stationLabel->setText(stationStr);
}

void SpeedometerController::setAccelerateFun()
{
    StopWatchView *view = speedometerView->stopWatchView;

//    qDebug() <<"加速"<<Qt::endl;
    if(view->direction == 0){
        view->myTimer->start(1);
        view->direction = 1;
    }
}

void SpeedometerController::setRetardFun()
{
//    qDebug() <<"减速"<<Qt::endl;
    StopWatchView *view = speedometerView->stopWatchView;
    view->direction = 0;
}


//设置停留时间
void SpeedometerController::setDwellTime(QString text)
{
    speedometerView->targetView->dwellTimeString->setText(text);
}
void SpeedometerController::timeout_slot()
{
    dwellTime --;
    //更新时间
    setDwellTime(QString::number(dwellTime));
    if(dwellTime <= 0) {
        obj_timer->stop();
    };
}

void SpeedometerController::distance_slot()
{
      distanceTime --;
    setDistanceString(distanceTime);
    if(distanceTime <=0){
        distance_timer->stop();
    }
}



